package com.ouc.common.utils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.*;

/**
 * @author B504
 */
public class ToPictureUtil {
  /**
   * 彩虹颜色等其他颜色的rgb数组
   */
  private static int[][] rainbow = new int[][]{
      {0, 0, 255}, {0, 2, 255}, {0, 4, 255}, {0, 7, 255}, {0, 9, 255}, {0, 12, 255}, {0, 14, 255}, {0, 17, 255}, {0, 19, 255},
      {0, 22, 255}, {0, 24, 255}, {0, 27, 255}, {0, 29, 255}, {0, 32, 255}, {0, 34, 255}, {0, 37, 255}, {0, 39, 255}, {0, 42, 255},
      {0, 44, 255}, {0, 47, 255}, {0, 49, 255}, {0, 52, 255}, {0, 54, 255}, {0, 57, 255}, {0, 59, 255}, {0, 62, 255}, {0, 64, 255},
      {0, 67, 255}, {0, 69, 255}, {0, 72, 255}, {0, 74, 255}, {0, 77, 255}, {0, 79, 255}, {0, 82, 255}, {0, 84, 255}, {0, 87, 255},
      {0, 89, 255}, {0, 92, 255}, {0, 94, 255}, {0, 97, 255}, {0, 99, 255}, {0, 102, 255}, {0, 104, 255}, {0, 107, 255}, {0, 109, 255},
      {0, 112, 255}, {0, 114, 255}, {0, 117, 255}, {0, 119, 255}, {0, 122, 255}, {0, 124, 255}, {0, 127, 255}, {0, 129, 255}, {0, 131, 255},
      {0, 134, 255}, {0, 136, 255}, {0, 139, 255}, {0, 141, 255}, {0, 144, 255}, {0, 146, 255}, {0, 149, 255}, {0, 151, 255}, {0, 154, 255},
      {0, 156, 255}, {0, 159, 255}, {0, 161, 255}, {0, 164, 255}, {0, 166, 255}, {0, 169, 255}, {0, 171, 255}, {0, 174, 255}, {0, 176, 255},
      {0, 179, 255}, {0, 181, 255}, {0, 184, 255}, {0, 186, 255}, {0, 189, 255}, {0, 191, 255}, {0, 194, 255}, {0, 196, 255}, {0, 199, 255},
      {0, 201, 255}, {0, 204, 255}, {0, 206, 255}, {0, 209, 255}, {0, 211, 255}, {0, 214, 255}, {0, 216, 255}, {0, 219, 255}, {0, 221, 255},
      {0, 224, 255}, {0, 226, 255}, {0, 229, 255}, {0, 231, 255}, {0, 234, 255}, {0, 236, 255}, {0, 239, 255}, {0, 241, 255}, {0, 244, 255},
      {0, 246, 255}, {0, 249, 255}, {0, 251, 255}, {0, 254, 255}, {0, 255, 254}, {0, 255, 252}, {0, 255, 250}, {0, 255, 249}, {0, 255, 247},
      {0, 255, 245}, {0, 255, 244}, {0, 255, 242}, {0, 255, 240}, {0, 255, 239}, {0, 255, 237}, {0, 255, 235}, {0, 255, 234}, {0, 255, 232},
      {0, 255, 230}, {0, 255, 229}, {0, 255, 227}, {0, 255, 225}, {0, 255, 224}, {0, 255, 222}, {0, 255, 220}, {0, 255, 219}, {0, 255, 217},
      {0, 255, 215}, {0, 255, 214}, {0, 255, 212}, {0, 255, 210}, {0, 255, 209}, {0, 255, 207}, {0, 255, 205}, {0, 255, 204}, {0, 255, 202},
      {0, 255, 200}, {0, 255, 199}, {0, 255, 197}, {0, 255, 195}, {0, 255, 194}, {0, 255, 192}, {0, 255, 190}, {0, 255, 189}, {0, 255, 187},
      {0, 255, 185}, {0, 255, 184}, {0, 255, 182}, {0, 255, 180}, {0, 255, 179}, {0, 255, 177}, {0, 255, 175}, {0, 255, 174}, {0, 255, 172},
      {0, 255, 170}, {0, 255, 169}, {0, 255, 167}, {0, 255, 166}, {0, 255, 164}, {0, 255, 162}, {0, 255, 161}, {0, 255, 159}, {0, 255, 157},
      {0, 255, 156}, {0, 255, 154}, {0, 255, 152}, {0, 255, 151}, {0, 255, 149}, {0, 255, 147}, {0, 255, 146}, {0, 255, 144}, {0, 255, 142},
      {0, 255, 141}, {0, 255, 139}, {0, 255, 137}, {0, 255, 136}, {0, 255, 134}, {0, 255, 132}, {0, 255, 131}, {0, 255, 129}, {0, 255, 127},
      {0, 255, 126}, {0, 255, 124}, {0, 255, 122}, {0, 255, 121}, {0, 255, 119}, {0, 255, 117}, {0, 255, 116}, {0, 255, 114}, {0, 255, 112},
      {0, 255, 111}, {0, 255, 109}, {0, 255, 107}, {0, 255, 106}, {0, 255, 104}, {0, 255, 102}, {0, 255, 101}, {0, 255, 99}, {0, 255, 97},
      {0, 255, 96}, {0, 255, 94}, {0, 255, 92}, {0, 255, 91}, {0, 255, 89}, {0, 255, 87}, {0, 255, 86}, {0, 255, 84}, {0, 255, 83}, {0, 255, 81},
      {0, 255, 79}, {0, 255, 78}, {0, 255, 76}, {0, 255, 74}, {0, 255, 73}, {0, 255, 71}, {0, 255, 69}, {0, 255, 68}, {0, 255, 66}, {0, 255, 64},
      {0, 255, 63}, {0, 255, 61}, {0, 255, 59}, {0, 255, 58}, {0, 255, 56}, {0, 255, 54}, {0, 255, 53}, {0, 255, 51}, {0, 255, 49}, {0, 255, 48},
      {0, 255, 46}, {0, 255, 44}, {0, 255, 43}, {0, 255, 41}, {0, 255, 39}, {0, 255, 38}, {0, 255, 36}, {0, 255, 34}, {0, 255, 33}, {0, 255, 31},
      {0, 255, 29}, {0, 255, 28}, {0, 255, 26}, {0, 255, 24}, {0, 255, 23}, {0, 255, 21}, {0, 255, 19}, {0, 255, 18}, {0, 255, 16}, {0, 255, 14},
      {0, 255, 13}, {0, 255, 11}, {0, 255, 9}, {0, 255, 8}, {0, 255, 6}, {0, 255, 4}, {0, 255, 3}, {0, 255, 1}, {0, 255, 0}, {1, 255, 0}, {3, 255, 0},
      {4, 255, 0}, {6, 255, 0}, {8, 255, 0}, {9, 255, 0}, {11, 255, 0}, {13, 255, 0}, {14, 255, 0}, {16, 255, 0}, {18, 255, 0}, {19, 255, 0}, {21, 255, 0},
      {23, 255, 0}, {24, 255, 0}, {26, 255, 0}, {28, 255, 0}, {29, 255, 0}, {31, 255, 0}, {33, 255, 0}, {34, 255, 0}, {36, 255, 0}, {38, 255, 0},
      {39, 255, 0}, {41, 255, 0}, {43, 255, 0}, {44, 255, 0}, {46, 255, 0}, {48, 255, 0}, {49, 255, 0}, {51, 255, 0}, {53, 255, 0}, {54, 255, 0},
      {56, 255, 0}, {58, 255, 0}, {59, 255, 0}, {61, 255, 0}, {63, 255, 0}, {64, 255, 0}, {66, 255, 0}, {68, 255, 0}, {69, 255, 0}, {71, 255, 0},
      {73, 255, 0}, {74, 255, 0}, {76, 255, 0}, {78, 255, 0}, {79, 255, 0}, {81, 255, 0}, {83, 255, 0}, {84, 255, 0}, {86, 255, 0}, {87, 255, 0},
      {89, 255, 0}, {91, 255, 0}, {92, 255, 0}, {94, 255, 0}, {96, 255, 0}, {97, 255, 0}, {99, 255, 0}, {101, 255, 0}, {102, 255, 0}, {104, 255, 0},
      {106, 255, 0}, {107, 255, 0}, {109, 255, 0}, {111, 255, 0}, {112, 255, 0}, {114, 255, 0}, {116, 255, 0}, {117, 255, 0}, {119, 255, 0}, {121, 255, 0},
      {122, 255, 0}, {124, 255, 0}, {126, 255, 0}, {127, 255, 0}, {129, 255, 0}, {131, 255, 0}, {132, 255, 0}, {134, 255, 0}, {136, 255, 0}, {137, 255, 0},
      {139, 255, 0}, {141, 255, 0}, {142, 255, 0}, {144, 255, 0}, {146, 255, 0}, {147, 255, 0}, {149, 255, 0}, {151, 255, 0}, {152, 255, 0}, {154, 255, 0},
      {156, 255, 0}, {157, 255, 0}, {159, 255, 0}, {161, 255, 0}, {162, 255, 0}, {164, 255, 0}, {166, 255, 0}, {167, 255, 0}, {169, 255, 0}, {170, 255, 0},
      {172, 255, 0}, {174, 255, 0}, {175, 255, 0}, {177, 255, 0}, {179, 255, 0}, {180, 255, 0}, {182, 255, 0}, {184, 255, 0}, {185, 255, 0}, {187, 255, 0},
      {189, 255, 0}, {190, 255, 0}, {192, 255, 0}, {194, 255, 0}, {195, 255, 0}, {197, 255, 0}, {199, 255, 0}, {200, 255, 0}, {202, 255, 0}, {204, 255, 0},
      {205, 255, 0}, {207, 255, 0}, {209, 255, 0}, {210, 255, 0}, {212, 255, 0}, {214, 255, 0}, {215, 255, 0}, {217, 255, 0}, {219, 255, 0}, {220, 255, 0},
      {222, 255, 0}, {224, 255, 0}, {225, 255, 0}, {227, 255, 0}, {229, 255, 0}, {230, 255, 0}, {232, 255, 0}, {234, 255, 0}, {235, 255, 0}, {237, 255, 0},
      {239, 255, 0}, {240, 255, 0}, {242, 255, 0}, {244, 255, 0}, {245, 255, 0}, {247, 255, 0}, {249, 255, 0}, {250, 255, 0}, {252, 255, 0}, {254, 255, 0},
      {255, 254, 0}, {255, 251, 0}, {255, 249, 0}, {255, 246, 0}, {255, 244, 0}, {255, 241, 0}, {255, 239, 0}, {255, 236, 0}, {255, 234, 0}, {255, 231, 0},
      {255, 229, 0}, {255, 226, 0}, {255, 224, 0}, {255, 221, 0}, {255, 219, 0}, {255, 216, 0}, {255, 214, 0}, {255, 211, 0}, {255, 209, 0}, {255, 206, 0},
      {255, 204, 0}, {255, 201, 0}, {255, 199, 0}, {255, 196, 0}, {255, 194, 0}, {255, 191, 0}, {255, 189, 0}, {255, 186, 0}, {255, 184, 0}, {255, 181, 0},
      {255, 179, 0}, {255, 176, 0}, {255, 174, 0}, {255, 171, 0}, {255, 169, 0}, {255, 166, 0}, {255, 164, 0}, {255, 161, 0}, {255, 159, 0}, {255, 156, 0},
      {255, 154, 0}, {255, 151, 0}, {255, 149, 0}, {255, 146, 0}, {255, 144, 0}, {255, 141, 0}, {255, 139, 0}, {255, 136, 0}, {255, 134, 0}, {255, 131, 0},
      {255, 129, 0}, {255, 127, 0}, {255, 124, 0}, {255, 122, 0}, {255, 119, 0}, {255, 117, 0}, {255, 114, 0}, {255, 112, 0}, {255, 109, 0}, {255, 107, 0},
      {255, 104, 0}, {255, 102, 0}, {255, 99, 0}, {255, 97, 0}, {255, 94, 0}, {255, 92, 0}, {255, 89, 0}, {255, 87, 0}, {255, 84, 0}, {255, 82, 0}, {255, 79, 0},
      {255, 77, 0}, {255, 74, 0}, {255, 72, 0}, {255, 69, 0}, {255, 67, 0}, {255, 64, 0}, {255, 62, 0}, {255, 59, 0}, {255, 57, 0}, {255, 54, 0}, {255, 52, 0},
      {255, 49, 0}, {255, 47, 0}, {255, 44, 0}, {255, 42, 0}, {255, 39, 0}, {255, 37, 0}, {255, 34, 0}, {255, 32, 0}, {255, 29, 0}, {255, 27, 0}, {255, 24, 0},
      {255, 22, 0}, {255, 19, 0}, {255, 17, 0}, {255, 14, 0}, {255, 12, 0}, {255, 9, 0}, {255, 7, 0}, {255, 4, 0}, {255, 2, 0}, {255, 2, 0}
  };
  private static final int[][] cooltowarm = new int[][]{
      {60, 78, 194}, {60, 79, 194}, {61, 80, 195}, {62, 81, 195}, {63, 82, 196}, {64, 83, 196}, {65, 84, 197}, {66, 85, 198}, {67, 86, 198}, {68, 87, 199}, {69, 88, 199}, {70, 89, 200},
      {71, 90, 201}, {72, 91, 201}, {72, 93, 202}, {73, 94, 202}, {74, 95, 203}, {75, 96, 204}, {76, 97, 204}, {77, 98, 205}, {78, 99, 205}, {79, 100, 206}, {80, 101, 207}, {81, 102, 207},
      {82, 103, 208}, {83, 104, 208}, {84, 105, 209}, {85, 107, 210}, {85, 108, 210}, {86, 109, 211}, {87, 110, 211}, {88, 111, 212}, {89, 112, 213}, {90, 113, 213}, {91, 114, 214}, {92, 115, 214},
      {93, 116, 215}, {94, 117, 216}, {95, 118, 216}, {96, 119, 217}, {97, 120, 217}, {98, 122, 218}, {98, 123, 219}, {99, 124, 219}, {100, 125, 220}, {101, 126, 220}, {102, 127, 221}, {103, 128, 221},
      {104, 129, 222}, {105, 130, 223}, {106, 131, 223}, {107, 132, 224}, {108, 133, 224}, {109, 134, 225}, {110, 136, 226}, {111, 137, 226}, {111, 138, 227}, {112, 139, 227}, {113, 140, 228},
      {114, 141, 229}, {115, 142, 229}, {116, 143, 230}, {117, 144, 230}, {118, 145, 231}, {119, 146, 232}, {120, 147, 232}, {121, 148, 233}, {122, 149, 233}, {123, 151, 234}, {124, 152, 235},
      {124, 153, 235}, {125, 154, 236}, {126, 155, 236}, {127, 156, 237}, {128, 157, 238}, {129, 158, 238}, {130, 159, 239}, {131, 160, 239}, {132, 161, 240}, {133, 162, 241}, {134, 163, 241},
      {135, 165, 242}, {136, 166, 242}, {137, 167, 243}, {137, 168, 244}, {138, 169, 244}, {139, 170, 245}, {140, 171, 245}, {141, 172, 246}, {142, 173, 247}, {143, 174, 247}, {144, 175, 248},
      {145, 176, 248}, {146, 177, 249}, {147, 178, 249}, {148, 180, 250}, {149, 181, 251}, {149, 182, 251}, {150, 183, 252}, {151, 184, 252}, {152, 185, 253}, {153, 186, 254}, {154, 187, 254},
      {155, 188, 254}, {155, 188, 254}, {156, 188, 254}, {156, 188, 254}, {156, 188, 253}, {157, 189, 253}, {157, 189, 253}, {158, 189, 253}, {158, 189, 253}, {159, 190, 252}, {159, 190, 252},
      {159, 190, 252}, {160, 190, 252}, {160, 190, 251}, {161, 191, 251}, {161, 191, 251}, {162, 191, 251}, {162, 191, 250}, {162, 191, 250}, {163, 192, 250}, {163, 192, 250}, {164, 192, 250},
      {164, 192, 249}, {164, 192, 249}, {165, 193, 249}, {165, 193, 249}, {166, 193, 248}, {166, 193, 248}, {167, 193, 248}, {167, 194, 248}, {167, 194, 248}, {168, 194, 247}, {168, 194, 247},
      {169, 195, 247}, {169, 195, 247}, {170, 195, 246}, {170, 195, 246}, {170, 195, 246}, {171, 196, 246}, {171, 196, 245}, {172, 196, 245}, {172, 196, 245}, {173, 196, 245}, {173, 197, 245},
      {173, 197, 244}, {174, 197, 244}, {174, 197, 244}, {175, 197, 244}, {175, 198, 243}, {175, 198, 243}, {176, 198, 243}, {176, 198, 243}, {177, 198, 243}, {177, 199, 242}, {178, 199, 242},
      {178, 199, 242}, {178, 199, 242}, {179, 200, 241}, {179, 200, 241}, {180, 200, 241}, {180, 200, 241}, {181, 200, 240}, {181, 201, 240}, {181, 201, 240}, {182, 201, 240}, {182, 201, 240},
      {183, 201, 239}, {183, 202, 239}, {184, 202, 239}, {184, 202, 239}, {184, 202, 238}, {185, 202, 238}, {185, 203, 238}, {186, 203, 238}, {186, 203, 238}, {186, 203, 237}, {187, 203, 237},
      {187, 204, 237}, {188, 204, 237}, {188, 204, 236}, {189, 204, 236}, {189, 205, 236}, {189, 205, 236}, {190, 205, 235}, {190, 205, 235}, {191, 205, 235}, {191, 206, 235}, {192, 206, 235},
      {192, 206, 234}, {192, 206, 234}, {193, 206, 234}, {193, 207, 234}, {194, 207, 233}, {194, 207, 233}, {195, 207, 233}, {195, 207, 233}, {195, 208, 232}, {196, 208, 232}, {196, 208, 232},
      {197, 208, 232}, {197, 208, 232}, {197, 209, 231}, {198, 209, 231}, {198, 209, 231}, {199, 209, 231}, {199, 210, 230}, {200, 210, 230}, {200, 210, 230}, {200, 210, 230}, {201, 210, 230},
      {201, 211, 229}, {202, 211, 229}, {202, 211, 229}, {203, 211, 229}, {203, 211, 228}, {203, 212, 228}, {204, 212, 228}, {204, 212, 228}, {205, 212, 227}, {205, 212, 227}, {206, 213, 227},
      {206, 213, 227}, {206, 213, 227}, {207, 213, 226}, {207, 213, 226}, {208, 214, 226}, {208, 214, 226}, {208, 214, 225}, {209, 214, 225}, {209, 215, 225}, {210, 215, 225}, {210, 215, 225},
      {211, 215, 224}, {211, 215, 224}, {211, 216, 224}, {212, 216, 224}, {212, 216, 223}, {213, 216, 223}, {213, 216, 223}, {214, 217, 223}, {214, 217, 222}, {214, 217, 222}, {215, 217, 222},
      {215, 217, 222}, {216, 218, 222}, {216, 218, 221}, {217, 218, 221}, {217, 218, 221}, {217, 218, 221}, {218, 219, 220}, {218, 219, 220}, {219, 219, 220}, {219, 219, 220}, {220, 220, 220},
      {220, 219, 219}, {220, 219, 218}, {220, 218, 218}, {220, 218, 217}, {220, 218, 217}, {221, 217, 216}, {221, 217, 216}, {221, 217, 215}, {221, 216, 214}, {221, 216, 214}, {221, 215, 213},
      {222, 215, 213}, {222, 215, 212}, {222, 214, 212}, {222, 214, 211}, {222, 214, 210}, {222, 213, 210}, {223, 213, 209}, {223, 212, 209}, {223, 212, 208}, {223, 212, 208}, {223, 211, 207},
      {223, 211, 206}, {224, 211, 206}, {224, 210, 205}, {224, 210, 205}, {224, 209, 204}, {224, 209, 204}, {224, 209, 203}, {225, 208, 203}, {225, 208, 202}, {225, 208, 201}, {225, 207, 201},
      {225, 207, 200}, {225, 207, 200}, {226, 206, 199}, {226, 206, 199}, {226, 205, 198}, {226, 205, 197}, {226, 205, 197}, {226, 204, 196}, {227, 204, 196}, {227, 204, 195}, {227, 203, 195},
      {227, 203, 194}, {227, 202, 193}, {227, 202, 193}, {228, 202, 192}, {228, 201, 192}, {228, 201, 191}, {228, 201, 191}, {228, 200, 190}, {228, 200, 189}, {229, 199, 189}, {229, 199, 188},
      {229, 199, 188}, {229, 198, 187}, {229, 198, 187}, {229, 198, 186}, {230, 197, 186}, {230, 197, 185}, {230, 196, 184}, {230, 196, 184}, {230, 196, 183}, {231, 195, 183}, {231, 195, 182},
      {231, 195, 182}, {231, 194, 181}, {231, 194, 180}, {231, 194, 180}, {232, 193, 179}, {232, 193, 179}, {232, 192, 178}, {232, 192, 178}, {232, 192, 177}, {232, 191, 176}, {233, 191, 176},
      {233, 191, 175}, {233, 190, 175}, {233, 190, 174}, {233, 189, 174}, {233, 189, 173}, {234, 189, 172}, {234, 188, 172}, {234, 188, 171}, {234, 188, 171}, {234, 187, 170}, {234, 187, 170},
      {235, 186, 169}, {235, 186, 169}, {235, 186, 168}, {235, 185, 167}, {235, 185, 167}, {235, 185, 166}, {236, 184, 166}, {236, 184, 165}, {236, 184, 165}, {236, 183, 164}, {236, 183, 163},
      {236, 182, 163}, {237, 182, 162}, {237, 182, 162}, {237, 181, 161}, {237, 181, 161}, {237, 181, 160}, {237, 180, 159}, {238, 180, 159}, {238, 179, 158}, {238, 179, 158}, {238, 179, 157},
      {238, 178, 157}, {238, 178, 156}, {239, 178, 155}, {239, 177, 155}, {239, 177, 154}, {239, 176, 154}, {239, 176, 153}, {239, 176, 153}, {240, 175, 152}, {240, 175, 152}, {240, 175, 151},
      {240, 174, 150}, {240, 174, 150}, {240, 173, 149}, {241, 173, 149}, {241, 173, 148}, {241, 172, 148}, {241, 172, 147}, {241, 172, 146}, {242, 171, 146}, {242, 171, 145}, {242, 171, 145},
      {242, 170, 144}, {242, 170, 144}, {242, 169, 143}, {243, 169, 142}, {243, 169, 142}, {243, 168, 141}, {243, 168, 141}, {243, 168, 140}, {243, 167, 140}, {244, 167, 139}, {244, 166, 139},
      {244, 166, 138}, {244, 166, 137}, {244, 165, 137}, {244, 165, 136}, {245, 165, 136}, {245, 164, 135}, {245, 164, 135}, {245, 163, 134}, {245, 163, 133}, {245, 163, 133}, {245, 162, 132},
      {245, 160, 131}, {244, 159, 130}, {243, 157, 129}, {243, 156, 128}, {242, 154, 127}, {241, 153, 127}, {241, 151, 126}, {240, 149, 125}, {239, 148, 124}, {239, 146, 123}, {238, 145, 122},
      {238, 143, 121}, {237, 142, 120}, {236, 140, 119}, {236, 139, 118}, {235, 137, 117}, {234, 135, 116}, {234, 134, 115}, {233, 132, 115}, {232, 131, 114}, {232, 129, 113}, {231, 128, 112},
      {230, 126, 111}, {230, 125, 110}, {229, 123, 109}, {228, 122, 108}, {228, 120, 107}, {227, 118, 106}, {227, 117, 105}, {226, 115, 104}, {225, 114, 103}, {225, 112, 102}, {224, 111, 102},
      {223, 109, 101}, {223, 108, 100}, {222, 106, 99}, {221, 104, 98}, {221, 103, 97}, {220, 101, 96}, {219, 100, 95}, {219, 98, 94}, {218, 97, 93}, {218, 95, 92}, {217, 94, 91}, {216, 92, 90},
      {216, 90, 89}, {215, 89, 89}, {214, 87, 88}, {214, 86, 87}, {213, 84, 86}, {212, 83, 85}, {212, 81, 84}, {211, 80, 83}, {210, 78, 82}, {210, 76, 81}, {209, 75, 80}, {209, 73, 79}, {208, 72, 78},
      {207, 70, 77}, {207, 69, 76}, {206, 67, 76}, {205, 66, 75}, {205, 64, 74}, {204, 63, 73}, {203, 61, 72}, {203, 59, 71}, {202, 58, 70}, {201, 56, 69}, {201, 55, 68}, {200, 53, 67}, {199, 52, 66}
      , {199, 50, 65}, {198, 49, 64}, {198, 47, 63}, {197, 45, 63}, {196, 44, 62}, {196, 42, 61}, {195, 41, 60}, {194, 39, 59}, {194, 38, 58}, {193, 36, 57}, {192, 35, 56}, {192, 33, 55},
      {191, 31, 54}, {190, 30, 53}, {190, 28, 52}, {189, 27, 51}, {189, 25, 50}, {188, 24, 50}, {187, 22, 49}, {187, 21, 48}, {186, 19, 47}, {185, 17, 46}, {185, 16, 45}, {184, 14, 44},
      {183, 13, 43}, {183, 11, 42}, {182, 10, 41}, {181, 8, 40}, {181, 7, 39}, {180, 5, 38}, {180, 5, 38},
  };
  private static int[][] blackbody = new int[][]{{0, 0, 0}, {1, 0, 0}, {2, 0, 0}, {3, 0, 0}, {4, 0, 0}, {5, 0, 0}, {7, 0, 0}, {8, 0, 0}, {9, 0, 0}, {10, 0, 0}, {11, 0, 0},
      {12, 0, 0}, {14, 0, 0}, {15, 0, 0}, {16, 0, 0}, {17, 0, 0}, {18, 0, 0}, {19, 0, 0}, {21, 0, 0}, {22, 0, 0}, {23, 0, 0}, {24, 0, 0}, {25, 0, 0}, {26, 0, 0}, {28, 0, 0},
      {29, 0, 0}, {30, 0, 0}, {31, 0, 0}, {32, 0, 0}, {33, 0, 0}, {35, 0, 0}, {36, 0, 0}, {37, 0, 0}, {38, 0, 0}, {39, 0, 0}, {41, 0, 0}, {42, 0, 0}, {43, 0, 0}, {44, 0, 0},
      {45, 0, 0}, {46, 0, 0}, {48, 0, 0}, {49, 0, 0}, {50, 0, 0}, {51, 0, 0}, {52, 0, 0}, {53, 0, 0}, {55, 0, 0}, {56, 0, 0}, {57, 0, 0}, {58, 0, 0}, {59, 0, 0}, {60, 0, 0},
      {62, 0, 0}, {63, 0, 0}, {64, 0, 0}, {65, 0, 0}, {66, 0, 0}, {67, 0, 0}, {69, 0, 0}, {70, 0, 0}, {71, 0, 0}, {72, 0, 0}, {73, 0, 0}, {75, 0, 0}, {76, 0, 0}, {77, 0, 0},
      {78, 0, 0}, {79, 0, 0}, {80, 0, 0}, {82, 0, 0}, {83, 0, 0}, {84, 0, 0}, {85, 0, 0}, {86, 0, 0}, {87, 0, 0}, {89, 0, 0}, {90, 0, 0}, {91, 0, 0}, {92, 0, 0}, {93, 0, 0},
      {94, 0, 0}, {96, 0, 0}, {97, 0, 0}, {98, 0, 0}, {99, 0, 0}, {100, 0, 0}, {101, 0, 0}, {103, 0, 0}, {104, 0, 0}, {105, 0, 0}, {106, 0, 0}, {107, 0, 0}, {108, 0, 0},
      {110, 0, 0}, {111, 0, 0}, {112, 0, 0}, {113, 0, 0}, {114, 0, 0}, {116, 0, 0}, {117, 0, 0}, {118, 0, 0}, {119, 0, 0}, {120, 0, 0}, {121, 0, 0}, {121, 0, 0},
      {122, 1, 0}, {123, 1, 0}, {124, 1, 0}, {124, 2, 0}, {125, 2, 0}, {126, 2, 0}, {126, 3, 0}, {127, 3, 0}, {128, 3, 0}, {129, 4, 0}, {129, 4, 0}, {130, 4, 0},
      {131, 5, 0}, {131, 5, 0}, {132, 5, 0}, {133, 6, 0}, {134, 6, 0}, {134, 6, 0}, {135, 7, 0}, {136, 7, 0}, {136, 7, 0}, {137, 8, 0}, {138, 8, 0}, {139, 8, 0},
      {139, 8, 0}, {140, 9, 0}, {141, 9, 0}, {141, 9, 0}, {142, 10, 0}, {143, 10, 0}, {144, 10, 0}, {144, 11, 0}, {145, 11, 0}, {146, 11, 0}, {146, 12, 0}, {147, 12, 0},
      {148, 12, 0}, {149, 13, 0}, {149, 13, 0}, {150, 13, 0}, {151, 14, 0}, {151, 14, 0}, {152, 14, 0}, {153, 15, 0}, {154, 15, 0}, {154, 15, 0}, {155, 16, 0},
      {156, 16, 0}, {156, 16, 0}, {157, 17, 0}, {158, 17, 0}, {159, 17, 0}, {159, 18, 0}, {160, 18, 0}, {161, 18, 0}, {161, 19, 0}, {162, 19, 0}, {163, 19, 0},
      {164, 20, 0}, {164, 20, 0}, {165, 20, 0}, {166, 21, 0}, {166, 21, 0}, {167, 21, 0}, {168, 22, 0}, {169, 22, 0}, {169, 22, 0}, {170, 22, 0}, {171, 23, 0},
      {171, 23, 0}, {172, 23, 0}, {173, 24, 0}, {174, 24, 0}, {174, 24, 0}, {175, 25, 0}, {176, 25, 0}, {177, 25, 0}, {177, 26, 0}, {178, 26, 0}, {179, 26, 0},
      {179, 27, 0}, {180, 27, 0}, {181, 27, 0}, {182, 28, 0}, {182, 28, 0}, {183, 28, 0}, {184, 29, 0}, {184, 29, 0}, {185, 29, 0}, {186, 30, 0}, {187, 30, 0},
      {187, 30, 0}, {188, 31, 0}, {189, 31, 0}, {189, 31, 0}, {190, 32, 0}, {191, 32, 0}, {192, 32, 0}, {192, 33, 0}, {193, 33, 0}, {194, 33, 0}, {194, 34, 0},
      {195, 34, 0}, {196, 34, 0}, {197, 35, 0}, {197, 35, 0}, {198, 35, 0}, {199, 36, 0}, {199, 36, 0}, {200, 36, 0}, {201, 36, 0}, {202, 37, 0}, {202, 37, 0},
      {203, 37, 0}, {204, 38, 0}, {204, 38, 0}, {205, 38, 0}, {206, 39, 0}, {207, 39, 0}, {207, 39, 0}, {208, 40, 0}, {209, 40, 0}, {209, 40, 0}, {210, 41, 0},
      {211, 41, 0}, {212, 41, 0}, {212, 42, 0}, {213, 42, 0}, {214, 42, 0}, {214, 43, 0}, {215, 43, 0}, {216, 43, 0}, {217, 44, 0}, {217, 44, 0}, {218, 44, 0},
      {219, 45, 0}, {219, 45, 0}, {220, 45, 0}, {221, 46, 0}, {222, 46, 0}, {222, 46, 0}, {223, 47, 0}, {224, 47, 0}, {224, 47, 0}, {225, 48, 0}, {226, 48, 0},
      {227, 48, 0}, {227, 49, 0}, {228, 49, 0}, {229, 49, 0}, {230, 50, 0}, {230, 51, 0}, {230, 52, 0}, {230, 54, 0}, {230, 55, 0}, {230, 56, 0}, {230, 58, 0},
      {231, 59, 0}, {231, 60, 0}, {231, 62, 0}, {231, 63, 0}, {231, 64, 0}, {231, 66, 0}, {232, 67, 0}, {232, 68, 0}, {232, 70, 0}, {232, 71, 0}, {232, 72, 0},
      {232, 74, 0}, {233, 75, 0}, {233, 76, 0}, {233, 78, 0}, {233, 79, 0}, {233, 80, 0}, {233, 82, 0}, {234, 83, 0}, {234, 84, 0}, {234, 86, 0}, {234, 87, 0},
      {234, 88, 0}, {234, 90, 0}, {235, 91, 0}, {235, 92, 0}, {235, 94, 0}, {235, 95, 0}, {235, 96, 0}, {235, 98, 0}, {236, 99, 0}, {236, 100, 0}, {236, 102, 0},
      {236, 103, 0}, {236, 104, 0}, {236, 106, 0}, {236, 107, 0}, {237, 108, 0}, {237, 110, 0}, {237, 111, 0}, {237, 112, 0}, {237, 114, 0}, {237, 115, 0}, {238, 116, 0},
      {238, 118, 0}, {238, 119, 0}, {238, 120, 0}, {238, 122, 0}, {238, 123, 0}, {239, 124, 0}, {239, 126, 0}, {239, 127, 0}, {239, 128, 0}, {239, 130, 0}, {239, 131, 0},
      {240, 132, 0}, {240, 134, 0}, {240, 135, 0}, {240, 136, 0}, {240, 138, 0}, {240, 139, 0}, {241, 140, 0}, {241, 142, 0}, {241, 143, 0}, {241, 144, 0}, {241, 146, 0},
      {241, 147, 0}, {242, 148, 0}, {242, 150, 0}, {242, 151, 0}, {242, 152, 0}, {242, 154, 0}, {242, 155, 0}, {243, 156, 0}, {243, 158, 0}, {243, 159, 0}, {243, 160, 0},
      {243, 162, 0}, {243, 163, 0}, {243, 164, 0}, {244, 166, 0}, {244, 167, 0}, {244, 168, 0}, {244, 170, 0}, {244, 171, 0}, {244, 172, 0}, {245, 174, 0}, {245, 175, 0},
      {245, 176, 0}, {245, 178, 0}, {245, 179, 0}, {245, 180, 0}, {246, 182, 0}, {246, 183, 0}, {246, 184, 0}, {246, 186, 0}, {246, 187, 0}, {246, 188, 0}, {247, 190, 0},
      {247, 191, 0}, {247, 192, 0}, {247, 194, 0}, {247, 195, 0}, {247, 196, 0}, {248, 198, 0}, {248, 199, 0}, {248, 200, 0}, {248, 202, 0}, {248, 203, 0}, {248, 204, 0},
      {249, 206, 0}, {249, 207, 0}, {249, 208, 0}, {249, 210, 0}, {249, 211, 0}, {249, 212, 0}, {250, 214, 0}, {250, 215, 0}, {250, 216, 0}, {250, 218, 0}, {250, 219, 0},
      {250, 220, 0}, {250, 222, 0}, {251, 223, 0}, {251, 224, 0}, {251, 226, 0}, {251, 227, 0}, {251, 228, 0}, {251, 230, 0}, {252, 231, 0}, {252, 232, 0}, {252, 234, 0},
      {252, 235, 0}, {252, 236, 0}, {252, 238, 0}, {253, 239, 0}, {253, 240, 0}, {253, 242, 0}, {253, 243, 0}, {253, 244, 0}, {253, 246, 0}, {254, 247, 0}, {254, 248, 0},
      {254, 250, 0}, {254, 251, 0}, {254, 252, 0}, {254, 254, 0}, {255, 255, 0}, {255, 255, 3}, {255, 255, 5}, {255, 255, 8}, {255, 255, 10}, {255, 255, 13}, {255, 255, 15},
      {255, 255, 18}, {255, 255, 20}, {255, 255, 23}, {255, 255, 25}, {255, 255, 28}, {255, 255, 30}, {255, 255, 33}, {255, 255, 35}, {255, 255, 38}, {255, 255, 40},
      {255, 255, 43}, {255, 255, 45}, {255, 255, 48}, {255, 255, 50}, {255, 255, 53}, {255, 255, 55}, {255, 255, 58}, {255, 255, 60}, {255, 255, 63}, {255, 255, 65},
      {255, 255, 68}, {255, 255, 70}, {255, 255, 73}, {255, 255, 75}, {255, 255, 78}, {255, 255, 80}, {255, 255, 83}, {255, 255, 85}, {255, 255, 88}, {255, 255, 90},
      {255, 255, 93}, {255, 255, 95}, {255, 255, 98}, {255, 255, 100}, {255, 255, 103}, {255, 255, 105}, {255, 255, 108}, {255, 255, 110}, {255, 255, 113}, {255, 255, 115},
      {255, 255, 118}, {255, 255, 120}, {255, 255, 123}, {255, 255, 125}, {255, 255, 127}, {255, 255, 130}, {255, 255, 132}, {255, 255, 135}, {255, 255, 137},
      {255, 255, 140}, {255, 255, 142}, {255, 255, 145}, {255, 255, 147}, {255, 255, 150}, {255, 255, 152}, {255, 255, 155}, {255, 255, 157}, {255, 255, 160},
      {255, 255, 162}, {255, 255, 165}, {255, 255, 167}, {255, 255, 170}, {255, 255, 172}, {255, 255, 175}, {255, 255, 177}, {255, 255, 180}, {255, 255, 182},
      {255, 255, 185}, {255, 255, 187}, {255, 255, 190}, {255, 255, 192}, {255, 255, 195}, {255, 255, 197}, {255, 255, 200}, {255, 255, 202}, {255, 255, 205},
      {255, 255, 207}, {255, 255, 210}, {255, 255, 212}, {255, 255, 215}, {255, 255, 217}, {255, 255, 220}, {255, 255, 222}, {255, 255, 225}, {255, 255, 227},
      {255, 255, 230}, {255, 255, 232}, {255, 255, 235}, {255, 255, 237}, {255, 255, 240}, {255, 255, 242}, {255, 255, 245}, {255, 255, 247}, {255, 255, 250},
      {255, 255, 252}, {255, 255, 252},
  };
  private static int[][] grayscale = new int[][]{{0, 0, 0}, {0, 0, 0}, {1, 1, 1}, {1, 1, 1}, {2, 2, 2}, {3, 3, 3}, {3, 3, 3}, {4, 4, 4}, {5, 5, 5}, {5, 5, 5}, {6, 6, 6}, {6, 6, 6}, {7, 7, 7},
      {8, 8, 8}, {8, 8, 8}, {9, 9, 9}, {10, 10, 10}, {10, 10, 10}, {11, 11, 11}, {11, 11, 11}, {12, 12, 12}, {13, 13, 13}, {13, 13, 13}, {14, 14, 14}, {15, 15, 15}, {15, 15, 15}, {16, 16, 16},
      {16, 16, 16}, {17, 17, 17}, {18, 18, 18}, {18, 18, 18}, {19, 19, 19}, {20, 20, 20}, {20, 20, 20}, {21, 21, 21}, {21, 21, 21}, {22, 22, 22}, {23, 23, 23}, {23, 23, 23}, {24, 24, 24},
      {25, 25, 25}, {25, 25, 25}, {26, 26, 26}, {26, 26, 26}, {27, 27, 27}, {28, 28, 28}, {28, 28, 28}, {29, 29, 29}, {30, 30, 30}, {30, 30, 30}, {31, 31, 31}, {31, 31, 31}, {32, 32, 32},
      {33, 33, 33}, {33, 33, 33}, {34, 34, 34}, {35, 35, 35}, {35, 35, 35}, {36, 36, 36}, {36, 36, 36}, {37, 37, 37}, {38, 38, 38}, {38, 38, 38}, {39, 39, 39}, {40, 40, 40}, {40, 40, 40},
      {41, 41, 41}, {41, 41, 41}, {42, 42, 42}, {43, 43, 43}, {43, 43, 43}, {44, 44, 44}, {44, 44, 44}, {45, 45, 45}, {46, 46, 46}, {46, 46, 46}, {47, 47, 47}, {48, 48, 48}, {48, 48, 48},
      {49, 49, 49}, {50, 50, 50}, {50, 50, 50}, {51, 51, 51}, {51, 51, 51}, {52, 52, 52}, {53, 53, 53}, {53, 53, 53}, {54, 54, 54}, {55, 55, 55}, {55, 55, 55}, {56, 56, 56}, {56, 56, 56},
      {57, 57, 57}, {58, 58, 58}, {58, 58, 58}, {59, 59, 59}, {60, 60, 60}, {60, 60, 60}, {61, 61, 61}, {61, 61, 61}, {62, 62, 62}, {63, 63, 63}, {63, 63, 63}, {64, 64, 64}, {64, 64, 64},
      {65, 65, 65}, {65, 65, 65}, {65, 65, 65}, {66, 66, 66}, {66, 66, 66}, {67, 67, 67}, {67, 67, 67}, {67, 67, 68}, {68, 68, 68}, {68, 68, 68}, {69, 69, 69}, {69, 69, 69}, {69, 69, 70},
      {70, 70, 70}, {70, 70, 70}, {71, 71, 71}, {71, 71, 71}, {72, 72, 72}, {72, 72, 72}, {72, 72, 73}, {73, 73, 73}, {73, 73, 73}, {74, 74, 74}, {74, 74, 74}, {74, 74, 75}, {75, 75, 75},
      {75, 75, 75}, {76, 76, 76}, {76, 76, 76}, {76, 76, 77}, {77, 77, 77}, {77, 77, 77}, {78, 78, 78}, {78, 78, 78}, {79, 79, 79}, {79, 79, 79}, {79, 79, 80}, {80, 80, 80}, {80, 80, 80},
      {81, 81, 81}, {81, 81, 81}, {81, 81, 82}, {82, 82, 82}, {82, 82, 83}, {83, 83, 83}, {83, 83, 83}, {83, 83, 84}, {84, 84, 84}, {84, 84, 85}, {85, 85, 85}, {85, 85, 85}, {85, 85, 86},
      {86, 86, 86}, {86, 86, 87}, {87, 87, 87}, {87, 87, 88}, {88, 88, 88}, {88, 88, 88}, {88, 88, 89}, {89, 89, 89}, {89, 89, 90}, {90, 90, 90}, {90, 90, 90}, {90, 90, 91}, {91, 91, 91},
      {91, 91, 92}, {92, 92, 92}, {92, 92, 93}, {92, 92, 93}, {93, 93, 93}, {93, 93, 94}, {94, 94, 94}, {94, 94, 95}, {95, 95, 95}, {95, 95, 95}, {95, 95, 96}, {96, 96, 96}, {96, 96, 97},
      {97, 97, 97}, {97, 97, 97}, {97, 97, 98}, {98, 98, 98}, {98, 98, 99}, {99, 99, 99}, {99, 99, 100}, {99, 99, 100}, {100, 100, 100}, {100, 100, 101}, {101, 101, 101}, {101, 101, 102},
      {101, 101, 102}, {102, 102, 103}, {102, 102, 103}, {103, 103, 103}, {103, 103, 104}, {104, 104, 104}, {104, 104, 105}, {104, 104, 105}, {105, 105, 105}, {105, 105, 106}, {106, 106, 106},
      {106, 106, 107}, {106, 106, 107}, {107, 107, 108}, {107, 107, 108}, {108, 108, 108}, {108, 108, 109}, {108, 108, 109}, {109, 109, 110}, {109, 109, 110}, {110, 110, 110}, {110, 110, 111},
      {111, 111, 111}, {111, 111, 112}, {111, 111, 112}, {112, 112, 113}, {112, 112, 113}, {113, 113, 113}, {113, 113, 114}, {113, 113, 114}, {114, 114, 115}, {114, 114, 115}, {115, 115, 115},
      {115, 115, 116}, {115, 115, 116}, {116, 116, 117}, {116, 116, 117}, {117, 117, 118}, {117, 117, 118}, {117, 117, 118}, {118, 118, 119}, {118, 118, 119}, {119, 119, 120}, {119, 119, 120},
      {120, 120, 120}, {120, 120, 121}, {120, 120, 121}, {121, 121, 122}, {121, 121, 122}, {122, 122, 123}, {122, 122, 123}, {122, 122, 123}, {123, 123, 124}, {123, 123, 124}, {124, 124, 125},
      {124, 124, 125}, {124, 124, 125}, {125, 125, 126}, {125, 125, 126}, {126, 126, 127}, {126, 126, 127}, {127, 127, 128}, {127, 127, 128}, {127, 127, 128}, {128, 128, 129}, {128, 128, 129},
      {129, 129, 130}, {129, 129, 130}, {129, 129, 130}, {130, 130, 131}, {130, 130, 131}, {131, 131, 132}, {131, 131, 132}, {132, 132, 132}, {132, 132, 133}, {132, 132, 133}, {133, 133, 134},
      {133, 133, 134}, {134, 134, 134}, {134, 134, 135}, {134, 134, 135}, {135, 135, 136}, {135, 135, 136}, {136, 136, 137}, {136, 136, 137}, {137, 137, 137}, {137, 137, 138}, {137, 137, 138},
      {138, 138, 139}, {138, 138, 139}, {139, 139, 139}, {139, 139, 140}, {139, 139, 140}, {140, 140, 141}, {140, 140, 141}, {141, 141, 141}, {141, 141, 142}, {142, 142, 142}, {142, 142, 143},
      {142, 142, 143}, {143, 143, 143}, {143, 143, 144}, {144, 144, 144}, {144, 144, 145}, {144, 144, 145}, {145, 145, 146}, {145, 145, 146}, {146, 146, 146}, {146, 146, 147}, {147, 147, 147},
      {147, 147, 148}, {147, 147, 148}, {148, 148, 148}, {148, 148, 149}, {149, 149, 149}, {149, 149, 150}, {149, 149, 150}, {150, 150, 150}, {150, 150, 151}, {151, 151, 151}, {151, 151, 152},
      {152, 152, 152}, {152, 152, 153}, {152, 152, 153}, {153, 153, 153}, {153, 153, 154}, {154, 154, 154}, {154, 154, 155}, {154, 154, 155}, {155, 155, 155}, {155, 155, 156}, {156, 156, 156},
      {156, 156, 157}, {157, 157, 157}, {157, 157, 157}, {157, 157, 158}, {158, 158, 158}, {158, 158, 159}, {159, 159, 159}, {159, 159, 159}, {159, 159, 160}, {160, 160, 160}, {160, 160, 161},
      {161, 161, 161}, {161, 161, 162}, {162, 162, 162}, {162, 162, 162}, {162, 162, 163}, {163, 163, 163}, {163, 163, 164}, {164, 164, 164}, {164, 164, 164}, {164, 164, 165}, {165, 165, 165},
      {165, 165, 166}, {166, 166, 166}, {166, 166, 166}, {167, 167, 167}, {167, 167, 167}, {167, 167, 168}, {168, 168, 168}, {168, 168, 169}, {169, 169, 169}, {169, 169, 169}, {169, 169, 170},
      {170, 170, 170}, {170, 170, 171}, {171, 171, 171}, {171, 171, 171}, {171, 171, 172}, {172, 172, 172}, {172, 172, 173}, {173, 173, 173}, {173, 173, 173}, {174, 174, 174}, {174, 174, 174},
      {174, 174, 175}, {175, 175, 175}, {175, 175, 175}, {176, 176, 176}, {176, 176, 176}, {177, 177, 177}, {177, 177, 177}, {177, 177, 178}, {178, 178, 178}, {178, 178, 178}, {179, 179, 179},
      {179, 179, 179}, {179, 179, 180}, {180, 180, 180}, {180, 180, 180}, {181, 181, 181}, {181, 181, 181}, {181, 181, 182}, {182, 182, 182}, {182, 182, 182}, {183, 183, 183}, {183, 183, 183},
      {184, 184, 184}, {184, 184, 184}, {184, 184, 185}, {185, 185, 185}, {185, 185, 185}, {186, 186, 186}, {186, 186, 186}, {187, 187, 187}, {187, 187, 187}, {187, 187, 187}, {188, 188, 188},
      {188, 188, 188}, {189, 189, 189}, {189, 189, 189}, {189, 189, 189}, {190, 190, 190}, {190, 190, 190}, {191, 191, 191}, {191, 191, 191}, {192, 192, 192}, {193, 193, 193}, {193, 193, 193},
      {194, 194, 194}, {195, 195, 195}, {195, 195, 195}, {196, 196, 196}, {196, 196, 196}, {197, 197, 197}, {198, 198, 198}, {198, 198, 198}, {199, 199, 199}, {200, 200, 200}, {200, 200, 200},
      {201, 201, 201}, {201, 201, 201}, {202, 202, 202}, {203, 203, 203}, {203, 203, 203}, {204, 204, 204}, {204, 204, 204}, {205, 205, 205}, {206, 206, 206}, {206, 206, 206}, {207, 207, 207},
      {208, 208, 208}, {208, 208, 208}, {209, 209, 209}, {210, 210, 210}, {210, 210, 210}, {211, 211, 211}, {211, 211, 211}, {212, 212, 212}, {213, 213, 213}, {213, 213, 213}, {214, 214, 214},
      {215, 215, 215}, {215, 215, 215}, {216, 216, 216}, {216, 216, 216}, {217, 217, 217}, {218, 218, 218}, {218, 218, 218}, {219, 219, 219}, {219, 219, 219}, {220, 220, 220}, {221, 221, 221},
      {221, 221, 221}, {222, 222, 222}, {223, 223, 223}, {223, 223, 223}, {224, 224, 224}, {225, 225, 225}, {225, 225, 225}, {226, 226, 226}, {226, 226, 226}, {227, 227, 227}, {228, 228, 228},
      {228, 228, 228}, {229, 229, 229}, {229, 229, 229}, {230, 230, 230}, {231, 231, 231}, {231, 231, 231}, {232, 232, 232}, {233, 233, 233}, {233, 233, 233}, {234, 234, 234}, {235, 235, 235},
      {235, 235, 235}, {236, 236, 236}, {236, 236, 236}, {237, 237, 237}, {238, 238, 238}, {238, 238, 238}, {239, 239, 239}, {240, 240, 240}, {240, 240, 240}, {241, 241, 241}, {241, 241, 241},
      {242, 242, 242}, {243, 243, 243}, {243, 243, 243}, {244, 244, 244}, {245, 245, 245}, {245, 245, 245}, {246, 246, 246}, {246, 246, 246}, {247, 247, 247}, {248, 248, 248}, {248, 248, 248},
      {249, 249, 249}, {250, 250, 250}, {250, 250, 250}, {251, 251, 251}, {251, 251, 251}, {252, 252, 252}, {253, 253, 253}, {253, 253, 253}, {254, 254, 254}, {254, 254, 254},
  };
  /**
   * 对颜色数组和相应的值进行map初始化
   */
  private static Map<Integer, int[]> map = new HashMap<>();

  /**
   * 对每一种颜色渐变方式设定相应的map集合
   *
   * @return
   */
  public static Map<Integer, int[]> getRainbow() {
    for (int i = 0; i < rainbow.length; i++) {
      map.put(i, rainbow[i]);
    }
    return map;
  }

  public static Map<Integer, int[]> getCooltowarm() {
    for (int i = 0; i < cooltowarm.length; i++) {
      map.put(i, cooltowarm[i]);
    }
    return map;
  }

  public static Map<Integer, int[]> getBlackbody() {
    for (int i = 0; i < blackbody.length; i++) {
      map.put(i, blackbody[i]);
    }
    return map;
  }

  public static Map<Integer, int[]> getGrayscale() {
    for (int i = 0; i < grayscale.length; i++) {
      map.put(i, grayscale[i]);
    }
    return map;
  }

  /**
   * 由二维数组生成图片
   *
   * @param data2D 二维数组
   * @param colors 颜色数组
   */
  public static BufferedImage colorImage(float[][] data2D, String[] colors) {
    //  根据色度带分组
    Integer group = colors.length - 1;
    BufferedImage bufImg = new BufferedImage(data2D.length, data2D[0].length, BufferedImage.TYPE_INT_ARGB);
    //设置画笔
    Graphics2D graphics = bufImg.createGraphics();
    ArrayList<Float> list = new ArrayList<>();
    for (int i = 0; i < data2D.length; i++) {
      for (int j = 0; j < data2D[i].length; j++) {
        list.add(data2D[i][j]);
      }
    }
    //  根据组数和所有数据进行分组，每一组就是分段的最大值和最小值（获取边界）
    List<List<Float>> subsection = subsection(list, group);
    System.out.println("list.size() = " + list.size());
    //  设置颜色RGB数组
    int[] ints;
    System.out.println("data2D.length = " + data2D.length);
    System.out.println("data2D[0].length = " + data2D[0].length);
    //  i是宽度，j是高度
    for (int i = 0; i < data2D.length; i++) {
      for (int j = 0; j < data2D[i].length; j++) {
        //  对于每一个数据判断是属于哪一组的
        for (int l = 0; l < subsection.size(); l++) {
          //  如果是NaN，设置透明
          if ((Float.valueOf(data2D[i][j])).isNaN()) {
            graphics.setColor(Color.LIGHT_GRAY);
            graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0));
            graphics.fillRect(i, j, 1, 1);
          }
          //  不是NaN查看它的数据范围，获取它的RGB数组并设置画笔颜色作图
          else if (data2D[i][j] >= subsection.get(l).get(0) && data2D[i][j] <= subsection.get(l).get(1)) {
            float[] boundary = new float[]{subsection.get(l).get(0), subsection.get(l).get(1)};
            ints = numToColor(data2D[i][j], boundary, colors[l], colors[l + 1]);
            graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1));
            graphics.setColor(new Color(ints[0], ints[1], ints[2]));
            graphics.fillRect(i, j, 1, 1);
          }
        }
      }
    }
    //  输出图片

    return rotateImage(bufImg, -90);

  }

  /**
   * 从某个数获取RGB，是插值插出来的
   * 通过起始颜色终止颜色，起始颜色的数值，终止颜色的数值，获取此数值的颜色（RGB）
   *
   * @param attribute  数值
   * @param boundary   范围（颜色左右最大值和最小值）
   * @param startColor 开始颜色
   * @param endColor   结束颜色
   * @return
   */
  public static int[] numToColor(float attribute, float[] boundary, String startColor, String endColor) {
    BigDecimal divisor = new BigDecimal(String.valueOf(boundary[1])).subtract(new BigDecimal(String.valueOf(boundary[0])));
    double percent = new BigDecimal(String.valueOf(attribute)).subtract(new BigDecimal(String.valueOf(boundary[0]))).divide(divisor, 4, BigDecimal.ROUND_HALF_UP).doubleValue();
    String interpolate = ColorUtil.interpolate(startColor, endColor, percent);
    return ColorUtil.getRgb(interpolate);
  }

  /**
   * 通过分组获取一堆数值的每一组的边界
   *
   * @param data  list数据
   * @param group 分的组数
   * @return 嵌套list，每一个总的list是一个范围，list[0]是最小值，list[1]是最大值
   */
  public static List<List<Float>> subsection(List<Float> data, Integer group) {
    //  新建嵌套list每个list是最大值和最小值（边界值）
    List<List<Float>> lists = new ArrayList<>();
    //  使用set不存储重复值（防止数据有重复值分组错误）
    Set<Float> set = new HashSet<>();
    for (int i = 0; i < data.size(); i++) {
      if (data.get(i).isNaN()) {
        continue;
      }
      set.add(data.get(i));
    }
    //  分组，每一组有多少个值
    Integer members = set.size() / group;
    //  用list排序，这样就能获得每一组的最大值和最小值
    List<Float> list = new ArrayList<>();
    for (Iterator it = set.iterator(); it.hasNext(); ) {
      list.add((Float) it.next());
    }
    Collections.sort(list);
    //  放入到lists集合中
    for (int i = 0; i < list.size(); i++) {
      ArrayList<Float> floats = new ArrayList<>();
      floats.add(list.get(i));
      i += (members - 1);
      if (i >= list.size()) {
        break;
      }
      floats.add(list.get(i));
      lists.add(floats);
    }
    return lists;
  }

  /**
   * 从数据中获取颜色的rgb数组
   * 从已经获得的颜色map中找
   *
   * @param colorMap 颜色map集合
   * @param data     归一化并转成0-512的数据
   * @return
   */
  public static int[] getColorFromData(Map<Integer, int[]> colorMap, int data) {
    int[] rgb = colorMap.get(data);
    return rgb;
  }

  /**
   * 使用归一化方式生成图片
   * 默认使用彩红颜色，但是如果指定colorType可以重新设置
   * 有cooltowarm，blackbody和grayscale形式
   *
   * @param data2D 二维数组（0-512）
   */
  public static BufferedImage toImageFromData(Integer[][] data2D, String colorType) {
    Map<Integer, int[]> colorMap = getRainbow();
    switch (colorType) {
      case "cooltowarm":
        colorMap = getCooltowarm();
        break;
      case "blackbody":
        colorMap = getBlackbody();
        break;
      case "grayscale":
        colorMap = getGrayscale();
        break;
      default:
        break;
    }
    BufferedImage bufImg = new BufferedImage(data2D.length, data2D[0].length, BufferedImage.TYPE_INT_ARGB);
    //设置画笔
    Graphics2D graphics = bufImg.createGraphics();
    for (int i = 0; i < data2D.length; i++) {
      for (int j = 0; j < data2D[i].length; j++) {
        if (data2D[i][j] == null) {
          graphics.setColor(Color.LIGHT_GRAY);
          graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0));
        } else {
          //  设置颜色RGB数组
          int[] ints = getColorFromData(colorMap, data2D[i][j]);
          graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 1));
          graphics.setColor(new Color(ints[0], ints[1], ints[2]));
        }
        graphics.fillRect(i, j, 1, 1);
      }

    }
    //  输出图片
    //  进行图片旋转，旋转-90度
    return rotateImage(bufImg, -90);

  }

  /**
   * 旋转图片，因为生成的图片和传的二维数组的原因很难正着写出图片
   * 使用这个方法将图片旋转一下，传给前台，可以直接进行贴图
   *
   * @param bufferedimage 图像缓存区
   * @param degree        旋转角度以度为单位，会将度转换为弧度
   * @return
   */
  public static BufferedImage rotateImage(BufferedImage bufferedimage,
                                          float degree) {
    int w = bufferedimage.getWidth(); // 得到图片宽度。
    int h = bufferedimage.getHeight();// 得到图片高度。
    BufferedImage img;// 空的图片。
    int x = (h / 2) - (bufferedimage.getWidth() / 2);// 确定原点坐标
    int y = (w / 2) - (bufferedimage.getHeight() / 2);
    AffineTransform at = new AffineTransform();
    at.rotate(Math.toRadians(degree), h / 2, w / 2);// 旋转图象
    at.translate(x, y);
    AffineTransformOp op = new AffineTransformOp(at,
        AffineTransformOp.TYPE_BICUBIC);
    img = new BufferedImage(h, w, bufferedimage.getType());
    img = op.filter(bufferedimage, img);

    return img;
  }

  /**
   * 从BufferedImage生成图片
   *
   * @param bufferedimage 图片的buffered资源
   * @param filePath      图片片绝对路径
   */
  public static void bufferImageToImage(BufferedImage bufferedimage, String filePath) {
    try {
      ImageIO.write(bufferedimage, "PNG", new File(filePath));
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * 从BufferedImage获取byte数组
   *
   * @param image 图片资源
   * @return byte数组
   * @throws IOException
   */
  public static byte[] bufferedImageToByteArray(BufferedImage image) throws IOException {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    ImageIO.write(image, "png", os);
    return os.toByteArray();
  }
}
